import java.io.*;
import java.sql.*;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.*;
import javax.servlet.http.*;

public class Main extends HttpServlet
{
	private String url;
	private Connection conn;
	private ResultSet rs;
	private Statement stmt;
	
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException 
    {	
    	res.setContentType("text/html");
    	PrintWriter out = res.getWriter();

    	out.print("<html><body>");
		
    	try 
    	{
    		Class.forName("com.mysql.jdbc.Driver");
    		url = "jdbc:mysql://localhost/student_db";
    		conn = DriverManager.getConnection(url, "root", "");
    		
    		stmt = conn.createStatement();
    		rs = stmt.executeQuery("Select * from student");
    		
    		out.print("<table border=\"1\">");
    		
    		out.print("<tr>");
    		out.print("<td>");
    		out.print("NAME");
    		out.print("</td>");
    		out.print("<td>");
    		out.print("ADDRESS");
    		out.print("</td>");
    		out.print("<td>");
    		out.print("TEL #");
    		out.print("</td>");
    		out.print("<td>");
    		out.print("</td>");
    		out.print("<td>");
    		out.print("</td>");
    		out.print("</tr>");
    		
    		while(rs.next())
    		{
    			String id = rs.getObject(1).toString();
    			
    			out.print("<tr>");
    			out.print("<td>");
    			out.print(rs.getObject(2).toString());
    			out.print("</td>");
    			out.print("<td>");
    			out.print(rs.getObject(3).toString());
    			out.print("</td>");
    			out.print("<td>");
    			out.print(rs.getObject(4).toString());
    			out.print("</td>");
    			out.print("<td>");
    			out.print("<a href=\"http://localhost:8080/Student/DeleteStudent?id=" + id + "\">Delete</a>");
    			out.print("</td>");
    			out.print("<td>");
    			out.print("<a href=\"http://localhost:8080/Student/UpdateStudentRecord?id=" + id + "\">Update</a>");
    			out.print("</td>");
    			out.print("</tr>");
    			
    		}
    		
    		out.print("</table>");
    		out.print("<a href=\"http://localhost:8080/Student/AddStudent\">Add New Student</a><br>");
   	
    	} 
    	catch (ClassNotFoundException cnfe) 
    	{
    			out.println("" + cnfe);
    	}
    	catch (SQLException sqle)
    	{
    			out.println("" + sqle);
    	}
    	finally
    	{
    		try
    		{
    			if(conn != null)
    			{
    				conn.close();
    			}
    		}
    		catch(SQLException sqle)
    		{
    			out.println("" + sqle);
    		}
    	}
    	
    	out.print("</body></html>");
		out.close();
    	
    }
}